package com.ocl.www.controller;

import com.ocl.www.domain.Letter;
import com.ocl.www.domain.User;
import com.ocl.www.service.LetterService;
import com.ocl.www.service.UserService;
import com.ocl.www.utils.UserUntil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/1.
 */
@RestController
@RequestMapping("/letter")
public class LetterController {

    private Map<String,Object> map = new HashedMap();

    @Autowired
    private UserService userService;
    @Autowired
    private LetterService letterService;

    @Autowired
    private EhCacheManager em;

    private Logger log = Logger.getLogger(LetterController.class);

    /**
     * 添加私信
     * @param maptemp
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertLetter", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value="添加一条私信", notes="添加一条私信，指定收信人id（post方式）")
    @ApiImplicitParam(name = "maptemp", value = "使用此格式：{}", required = true, dataType = "Map")
    public Map<String,Object> insertLetter(@RequestBody Map<String,Object> maptemp){

        int userid = UserUntil.getUserId(SecurityUtils.getSubject().getPrincipals().asSet());

        int c =0 ;
        try {
            Letter letter = new Letter();
            letter.setTitle(String.valueOf(maptemp.get("title")));
            letter.setContent(String.valueOf(maptemp.get("content")));
            letter.setReciverId(Integer.valueOf(maptemp.get("reciverId").toString()));
            letter.setReciverName(String.valueOf(maptemp.get("reciverName")));
            letter.setSenderId(userid);
            User user = new User();
            user.setUserId(userid);
            user = userService.findUserInfo(user);
            letter.setSenderName(user.getNickName());

            c = letterService.insertLetter(letter);
            if (c==1){
                map.put("info","s");
                map.put("tip","私信发送成功！");
                //em.getCache("shiroCache").put("letterForUser-"+maptemp.get("reciverId"),"true");
                //log.info("已经将私信发送出去了："+em.getCache("shiroCache").get("letterForUser-"+maptemp.get("reciverId")));
                return map;

            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("info","e");
            map.put("tip",e.getMessage());
            return map;
        }
        //letter=null;
        return map;
    }


    /**
     * 查询给我的未读私信
     * @param maptemp
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectLettersWithReciverId", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value="查询接收人的所有私信记录", notes="查询接收人的所有私信记录，post方式")
    @ApiImplicitParam(name = "maptemp", value = "使用此格式：{}", required = true, dataType = "Map")
    public Map<String,Object> selectLettersWithReciverId(@RequestBody Map<String,Object> maptemp){
        Letter letter = new Letter();
        letter.setReciverId(UserUntil.getUserId(SecurityUtils.getSubject().getPrincipals().asSet()));
        letter.setIsread("0");
        List<Letter> letterList = null;
        try {
            letterList = letterService.selectLettersWithReciverId(letter);
            maptemp.put("info","s");
            maptemp.put("letterList",letterList);
        } catch (Exception e) {
            maptemp.put("info","e");
            maptemp.put("tip",e.getMessage());
        }
        return maptemp;
    }


    /**
     * 设置私信为已读：isread = 1
     * @param maptemp
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateLetterOfIsread", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value="更新私信记录为已读", notes="更新私信记录为已读，post方式")
    @ApiImplicitParam(name = "maptemp", value = "使用此格式：{}", required = true, dataType = "Map")
    public Map<String,Object> updateLetterOfIsread(@RequestBody Map<String,Object> maptemp){

        Letter letter = new Letter();
        letter.setId(Integer.valueOf(maptemp.get("id").toString()));
        int count = 0;
        try {
            count = letterService.updateLetterOfIsread(letter);
            maptemp.put("info","s");
            maptemp.put("tip",count);
        } catch (Exception e) {
            maptemp.put("info","e");
            maptemp.put("tip",e.getMessage());
        }

        return maptemp;
    }

}
